import Vue from 'vue';
import UniRouteGuards from '@/common/uniapp-route-guards';
Vue.use(UniRouteGuards);
const install = (Vue, vm) => {
    const guard = new UniRouteGuards();
    /**
     * 自定义路由拦截白名单
     */
    const WHILE_LIST = ['/pages/index/index', '/pages/activity/activity', '/pages/my/my'];
    /**
     * 全局前置守卫
     */
    guard.beforeEach((to, from, next) => {
        // console.log("to: ", to);
        // console.log("from: ", from);
        if (WHILE_LIST.includes(to.url)) {
            return next(to.url);
        }
        if (to.action == 'navigateBack' ) {
           return next();
        }
        const token = uni.getStorageSync('token');
        
        if (!token) {
            vm.$u.vuex('vuex_login', 1)
        } else {
            vm.$u.vuex('vuex_login', 0)
            next();
        }
    });

    // 拦截 调用 uni.switchTab 页面C并跳转到 页面D
    guard.beforeEach((to, from, next) => {
        if (to.action === 'switchTab' && to.url === '/src/pages/c') {
            return next({
                url: '/src/pages/d',
                action: 'navigateTo'
            });
        }
        next();
    });
    /**
     * 全局后置后卫
     */
    guard.afterEach((to, from) => {});

}
export default {
    install
}
